(function($) {
	$.fn.sim_tree = function(opts) {
		$("#department-treeview").html("");
		setting = $.extend({
			data: {},
			callback: function() {
			}
		},opts|| {});

		var html = getTreeNodeBase(setting.data,setting);
		$(this).addClass("tree");
		$(this).addClass("tree-unselectable");
		$(this).attr("role", "tree");
		$(this).html(html);
		$(".tree-branch-header").on("click", function(evt) {
			if($(this).attr("show") == "show") {
				$(this).attr("show","hide");
				$(this).siblings().each( function() {
					$(this).css("display","none");
				});
			} else if($(this).attr("show") == "hide") {
				$(this).attr("show","show");
				$(this).siblings().each( function() {
					$(this).css("display","");
				});
			}
			return false;
			// evt.preventDefault();
		});
		
		setting.callback.apply();
	};
	var getTreeNodeBase = function(data,setting) {
		var html = "<li class='tree-branch' role='treeitem' aria-expanded='false'> "
		+ "<div class='tree-branch-header' show='show'>"
		+"<div class='dep'>";
		if(setting.extend && setting.extend.position=="before") {
			var extend_html = "";
			if(setting.extend.tag) {
				extend_html += "<"+setting.extend.tag;
				if(setting.extend.attr) {
					for(key in setting.extend.attr) {
						extend_html += " "+key+"='"+setting.extend.attr[key]+"'";
					}
				}
				if(setting.extend.param) {
					for(var i=0;i<setting.extend.param.length;i++) {
						var param = setting.extend.param[i];
						if(data[param])
							extend_html += " "+param+"='"+data[param]+"'";
					}
				}
				if(setting.extend.text)
					extend_html += ">"+setting.extend.text+"</"+setting.extend.tag+">";
				else
					extend_html += "/>";
				console.log(extend_html);
				html += extend_html;
			}
		}
		html += "<span class='tree-branch-name'> "
		+ "<i class='icon-folder ace-icon fa fa-folder "
		+ getColor()
		+ "'></i> "
		+ "<span class='tree-label'>"
		+ data['text']
		+ "</span> "
		+ "</span> ";
		if(setting.extend && setting.extend.position=="after") {
			var extend_html = "";
			if(setting.extend.tag) {
				extend_html += "<"+setting.extend.tag;
				if(setting.extend.attr) {
					for(key in setting.extend.attr) {
						extend_html += " "+key+"='"+setting.extend.attr[key]+"'";
					}
				}
				if(setting.extend.param) {
					for(var i=0;i<setting.extend.param.length;i++) {
						var param = setting.extend.param[i];
						if(data[param])
							extend_html += " "+param+"='"+data[param]+"'";
					}
				}
				if(setting.extend.text)
					extend_html += ">"+setting.extend.text+"</"+setting.extend.tag+">";
				else
					extend_html += "/>";
				console.log(extend_html);
				html += extend_html;
			}
		}
		html+="</div>"
		+ "</div>"
		+ "<ul class='tree-branch-children' role='group'> ";

		var addition = data['additionalParameters'];
		if (addition) {
			var child = addition['children'];
			for ( var childNode in child) {
				html += getTreeNodeBase(child[childNode],setting);
			}
		}
		html += "</ul> " + "<div class='tree-loader hide' role='alert'> "
		+ "<div class='tree-loading'>"
		+ "<i class='ace-icon fa fa-refresh fa-spin blue'></i>"
		+ "</div> " + "</div> " + "</li>";

		return html;
	}
	function GetRandomNum(Min, Max) {
		var Range = Max - Min;
		var Rand = Math.random();
		return (Min + Math.round(Rand * Range));
	}

	function getColor() {
		var colors = [ 'red', 'orange', 'blue', 'green', 'black' ];
		var num = GetRandomNum(1, 5);
		return colors[num];
	}

})(jQuery);